package com.swiftops.basic.example;

import com.swiftops.common.annotation.IgnoreSecurity;
import com.swiftops.common.core.ApiResult;
import com.swiftops.common.core.PageResult;
import com.swiftops.common.core.Result;
import com.swiftops.common.util.BindingResultUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.ArrayList;

@Slf4j
@RestController
@RequestMapping(value = "/api/v1/example")
public class ExampleController {

    @PermitAll
    @ApiOperation(value = "返回请求信息")
    @RequestMapping(value = "/pc/test", method = RequestMethod.GET)
    public ResponseEntity<String> test() {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("890");
    }

    @PermitAll
    @ApiOperation(value = "返回请求信息")
    @RequestMapping(value = "/pc/test1", method = RequestMethod.GET)
    public ResponseEntity<Result<Object>> test1() {
        ResponseEntity.ok(Result.ok("00"));
        ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Result.ok("SERVER_ERROR"));
        new ResponseEntity<>(Result.ok("ok"), HttpStatus.OK);

        return new ResponseEntity<>(Result.ok("ok"), HttpStatus.OK);
    }

    @PermitAll
    @ApiOperation(value = "返回请求信息")
    @RequestMapping(value = "/pc/test2", method = RequestMethod.GET)
    public Result<Object> test2() {
        return Result.ok("ok");
    }

    @PermitAll
    @ApiOperation(value = "返回请求信息")
    @RequestMapping(value = "/pc/test3", method = RequestMethod.GET)
    public String test3() {
        return "ok";
    }

    @ApiOperation(value = "返回请求信息")
    @RequestMapping(value = "/pc/test4", method = RequestMethod.GET)
    public ApiResult test4() {
        ArrayList<ExampleRequestVO> arrayList = new ArrayList<>();

        PageResult<ExampleRequestVO> success = PageResult.of(1, arrayList);
        return ApiResult.success("ok");
    }

    @IgnoreSecurity
    @ApiOperation(value = "返回请求信息")
    @RequestMapping(value = "/pc/test5", method = RequestMethod.POST)
    public ResponseEntity<ApiResult<Object>> test5(@Valid @RequestBody ExampleRequestVO request, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtil.getErrorMessage(bindingResult);
        }
        try {
            System.out.println(request);
            return ApiResult.ok(request);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @Autowired
    ApplicationContext applicationContext;
    @Autowired
    RedisTemplate redisTemplate;

    @IgnoreSecurity
    @ApiOperation(value = "返回请求信息")
    @RequestMapping(value = "/pc/test5", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<Object>> test6() {

        System.out.println("redisTemplate=" + redisTemplate.hashCode());
        RedisTemplate template = applicationContext.getBean("redisTemplate", RedisTemplate.class);
        System.out.println("template=" + template.hashCode());

        return ApiResult.ok();
    }

    @PostConstruct
    public void pos() {
        System.out.println("PostConstruct");
        System.out.println("redisTemplate=" + redisTemplate.hashCode());
    }
}
